package com.yd.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.pojo.Banner;
import com.yd.service.BannerService;
import com.yd.tool.UtsReg;

@Controller
public class BannerControllerSys {
	@Resource
	private BannerService bannerService;
	
	@RequestMapping(value = {"/sys/banner/list"})
	@ResponseBody
	public String sys_banner_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String so="";
		String tid="";
		String page="";
		String size="";
		if(UtsReg.getStringKB(request.getParameter("so"))){
			so=request.getParameter("so").trim();
		}
		if(UtsReg.getIntXl(request.getParameter("tid"))){
			tid=request.getParameter("tid").trim();
		}
		if(UtsReg.getIntXl(request.getParameter("page"))){
			page=request.getParameter("page").trim();
		}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){
			size=request.getParameter("size").trim();
		}else{size = "10";}
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<Banner> list = null;
		EntityWrapper<Banner> ew=new EntityWrapper<Banner>();
		if(so.length()>0){
			ew.like("title", so);
		}
		if(tid.length()>0){
			ew.eq("tid", tid);
		}
		list=bannerService.selectList(ew.orderBy("id",false));
		PageInfo<Banner> pages = new PageInfo<Banner>(list);
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	
	@RequestMapping("/sys/banner/add")
	@ResponseBody
	public String sys_banner_add(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject(); 

		String title="";
		String img="";
		String url="";
		String tid="";
		
		if(UtsReg.getStringTitle(request.getParameter("title"))){
			title=request.getParameter("title").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "标题格式错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringGB(request.getParameter("img"))){
			img=request.getParameter("img").trim();
			String[] imgs=img.split(",");
			img=imgs[0];
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "图片格式错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringUrl(request.getParameter("url"))){
			url=request.getParameter("url").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "URL格式错误");return jsonObject.toString();
		}
			
		if(UtsReg.getIntXl(request.getParameter("tid"))){
			tid=request.getParameter("tid").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "类别格式错误");return jsonObject.toString();
		}
		
		Banner banner=new Banner();
		banner.setTitle(title);
		banner.setImg(img);
		banner.setUrl(url);
		banner.setTid(tid);
		
		if(!bannerService.insert(banner)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping("/sys/banner/upbyid")
	@ResponseBody
	public String sys_banner_upbyid(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject(); 

		String id="";
		String title="";
		String img="";
		String url="";
		String tid="";
		
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "参数错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringTitle(request.getParameter("title"))){
			title=request.getParameter("title").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "标题格式错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringGB(request.getParameter("img"))){
			img=request.getParameter("img").trim();
			String[] imgs=img.split(",");
			img=imgs[0];
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "图片格式错误");return jsonObject.toString();
		}
		
		if(UtsReg.getStringUrl(request.getParameter("url"))){
			url=request.getParameter("url").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "URL格式错误");return jsonObject.toString();
		}
		
		if(UtsReg.getIntXl(request.getParameter("tid"))){
			tid=request.getParameter("tid").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "类别格式错误");return jsonObject.toString();
		}
		
		Banner banner=new Banner();
		banner.setId(Integer.valueOf(id));
		banner.setTitle(title);
		banner.setImg(img);
		banner.setUrl(url);
		banner.setTid(tid);
		
		if(!bannerService.updateById(banner)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/sys/banner/delbyid"})
	@ResponseBody
	public String sys_banner_delbyid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String id="";
		if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(bannerService.selectCount(new EntityWrapper<Banner>().eq("id", id))<1){
			jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		if(!bannerService.deleteById(id)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
}
